<extend name="Layout/ins_page" />

<block name="head">
</block>

<block name="js">


</block>

<block name="content">



    <!--content begin-->
    <style type="text/css">
        table
        {
            border-collapse:collapse; width:60%
        }
        table, td, th
        {
            border:1px solid black;
        }
        td{width:10%}
    </style>
    <script src="Public/Scripts/jquery-1.8.3.js"></script>
    <script src="Public/Scripts/timejs.js"></script>

    <script language="javascript" type="text/javascript" src="Public/Scripts/tablesort2/jquery.tablesorter.js"></script>

    <script>
        function unique(arr) {
            var result = [], hash = {};
            for (var i = 0, elem; (elem = arr[i]) != null; i++) {
                if (!hash[elem]) {
                    result.push(elem);
                    hash[elem] = true;
                }
            }
            return result;
        }

        //echars画图用
        window.linetotalArray=[];
        window.linetotalbrand=[];
        window.QuantityArray=[];
        window.Quantitybrand=[];
        window.profitArray=[];
        window.profitbrand=[];



        function inputparacheck(input){
            var thisRegex = new RegExp('^[0-9]{1,3}$');
            if(!thisRegex.test(input)){
                alert('参数为0-999的正整数');
            }
        }

    </script>

    <script language="JavaScript">

        function linetoaldata(TopCount,BackMonth){
            postquery='linetotaldo'+TopCount+'do'+BackMonth;
            $.ajax({
                url: "/Report/topbrandService",
                type: "POST",
                dataType: 'jsonp',
                jsonp: 'postquery',
                jsonpCallback: postquery,
                timeout: 200000,
                success: function (jsonp) {
                    //console.log(jsonp);
                    datalinetotal=jsonp;
                    //console.log(datalinetotal);
                    //alert(datalinetotal.length);
                    for (var i=0;i<datalinetotal.length;i++)
                    {
                        linetotalbrand[i]=datalinetotal[i]['topbrand'];//加入行标题
                    }
                    linetotalbrand=unique(linetotalbrand);	//行标题去重
                    //linetotalbrand[]='排名外品牌';
                    for (var i=0;i<linetotalbrand.length;i++)
                    {
                        document.getElementById('linetoalTable').rows[i+1].cells[0].innerHTML=linetotalbrand[i]
                    }
                    for (var i=0;i<datalinetotal.length;i++)
                    {
                        //console.log(linetotalbrand);
                        for(var imonth=0;imonth<linetotalheads.length;imonth++)
                        {
                            //alert(linetotalheads[imonth]);
                            if (datalinetotal[i]['ymonth']==linetotalheads[imonth])
                            {
                                //alert(linetotalheads[imonth]);
                                document.getElementById('linetoalTable').rows[linetotalbrand.indexOf(datalinetotal[i]['topbrand'])+1].cells[imonth+1].innerHTML=datalinetotal[i]['linetotal']
//				//console.log(datalinetotal[i]['linetotal'])
                            }
                        }
                    }
                    //console.log ();

                    //console.log(datalinetotal);
                }/*对jsonp返回函数的*/
            }/*对AJAX*/);  }


        function Quantitydata(TopCount,BackMonth){
            postquery='Quantitydo'+TopCount+'do'+BackMonth;
            $.ajax({
                url: "/Report/topbrandService",
                type: "POST",
                dataType: 'jsonp',
                jsonp: 'postquery',
                jsonpCallback: postquery,
                timeout: 200000,
                success: function (jsonp) {
                    dataQuantity=jsonp;
                    //alert(dataQuantity.length);
                    for (var i=0;i<dataQuantity.length;i++)
                    {
                        Quantitybrand[i]=dataQuantity[i]['topbrand'];//加入行标题
                    }
                    Quantitybrand=unique(Quantitybrand);	//行标题去重
                    for (var i=0;i<Quantitybrand.length;i++)
                    {
                        document.getElementById('QuantityTable').rows[i+1].cells[0].innerHTML=Quantitybrand[i]
                    }
                    for (var i=0;i<dataQuantity.length;i++)
                    {
                        for(var imonth=0;imonth<Quantityheads.length;imonth++)
                        {
                            //alert(linetotalheads[imonth]);
                            if (dataQuantity[i]['ymonth']==Quantityheads[imonth])
                            {
                                //alert(linetotalheads[imonth]);
                                document.getElementById('QuantityTable').rows[Quantitybrand.indexOf(dataQuantity[i]['topbrand'])+1].cells[imonth+1].innerHTML=dataQuantity[i]['Quantity']
                            }
                        }
                    }
                }/*对jsonp返回函数的*/
            }/*对AJAX*/);  }

        function profitdata(TopCount,BackMonth){
            postquery='profitdo'+TopCount+'do'+BackMonth;
            $.ajax({
                url: "/Report/topbrandService",
                type: "POST",
                dataType: 'jsonp',
                jsonp: 'postquery',
                jsonpCallback: postquery,
                timeout: 200000,
                success: function (jsonp) {
                    dataprofit=jsonp;
                    //alert(dataprofit.length);
                    for (var i=0;i<dataprofit.length;i++)
                    {
                        profitbrand[i]=dataprofit[i]['topbrand'];//加入行标题
                    }
                    profitbrand=unique(profitbrand);	//行标题去重
                    for (var i=0;i<profitbrand.length;i++)
                    {
                        document.getElementById('profitTable').rows[i+1].cells[0].innerHTML=profitbrand[i]
                    }
                    for (var i=0;i<dataprofit.length;i++)
                    {
                        for(var imonth=0;imonth<profitheads.length;imonth++)
                        {
                            //alert(linetotalheads[imonth]);
                            if (dataprofit[i]['ymonth']==linetotalheads[imonth])
                            {
                                //alert(linetotalheads[imonth]);
                                document.getElementById('profitTable').rows[profitbrand.indexOf(dataprofit[i]['topbrand'])+1].cells[imonth+1].innerHTML=dataprofit[i]['profit']
                            }
                        }
                    }
                }/*对jsonp返回函数的*/
            }/*对AJAX*/);  }

    </script>

    <script>


        function querydb(){
            var tablemain =document.getElementById("linetoalTable");
            //console.log(rows);
            if ( tablemain.rows.length>1) {
                for ( var i = tablemain.rows.length-1; i>0; i--)
                {
                    tablemain.deleteRow (i);
                }		}
            var tablemain =document.getElementById("QuantityTable");
            //console.log(rows);
            if ( tablemain.rows.length>1) {
                for ( var i = tablemain.rows.length-1; i>0; i--)
                {
                    tablemain.deleteRow (i);
                }		}
            var tablemain =document.getElementById("profitTable");
            //console.log(rows);
            if ( tablemain.rows.length>1) {
                for ( var i = tablemain.rows.length-1; i>0; i--)
                {
                    tablemain.deleteRow (i);
                }		}
            var tabletitle =document.getElementById("linetoalTableHead");
            if (tabletitle.cells.length>1) {
                for ( var i = tabletitle.cells.length-1; i>0; i--)
                {
                    tabletitle.deleteCell (i);
                }		}
            var tabletitle =document.getElementById("QuantityTableHead");
            if (tabletitle.cells.length>1) {
                for ( var i = tabletitle.cells.length-1; i>0; i--)
                {
                    tabletitle.deleteCell (i);
                }		}
            var tabletitle =document.getElementById("profitTableHead");
            if (tabletitle.cells.length>1) {
                for ( var i = tabletitle.cells.length-1; i>0; i--)
                {
                    tabletitle.deleteCell (i);
                }		}
            var TopCount=document.getElementById('TopCount').value
            var BackMonth=document.getElementById('BackMonth').value

            drawtable(TopCount,BackMonth);
            linetoaldata(TopCount,BackMonth);
            Quantitydata(TopCount,BackMonth);
            profitdata(TopCount,BackMonth);
        }
        function drawtable(TopCount,BackMonth){
            window.linetotalheads=[];
            window.Quantityheads=[];
            window.profitheads=[];
//	var TopCount=document.getElementById('TopCount').value
//	var BackMonth=document.getElementById('BackMonth').value

            linetotalheads.length=0;
            Quantityheads.length=0;
            profitheads.length=0;

            for(var i=0 ; i<=BackMonth;i++){
                var ths1=document.getElementById('linetoalTableHead').insertCell()
                var ths2=document.getElementById('QuantityTableHead').insertCell()
                var ths3=document.getElementById('profitTableHead').insertCell()
                var m=new Date();
                var m=DateAdd('m',-i,);
                linetotalheads[i] = m.Format("yy/MM");
                Quantityheads[i] = m.Format("yy/MM");
                profitheads[i] = m.Format("yy/MM");
                ths1.innerHTML=m.Format("yy/MM");
                ths2.innerHTML=m.Format("yy/MM");
                ths3.innerHTML=m.Format("yy/MM");
            }//画列头

            for (var i=0 ; i<TopCount; i++){
                var trs1=document.getElementById('linetoalTable').insertRow();
                var trs2=document.getElementById('QuantityTable').insertRow();
                var trs3=document.getElementById('profitTable').insertRow();
                for(var irow=0;irow<=Number(BackMonth)+1;irow++)
                {
                    trs1.insertCell();
                    trs2.insertCell();
                    trs3.insertCell();
                }
            }//画数据行再画数据列


        }

    </script>






    <div>
        排名<input id='TopCount' maxlength="3" value="6" onkeyup="inputparacheck(this.value)" >
        </input>
        </select>
        回溯月份
        <input id='BackMonth' maxlength="2" value="6" onkeyup="inputparacheck(this.value)"></input>
        <button onclick='querydb();'>查询</button>
    </div>
    </br>
    <div id="linetoalTableContainer">
        <table id='linetoalTable' class="tablesorter" onload="drawtable(6,6);linetoaldata(6,6)" ><thead>
        <tr id='linetoalTableHead'>
            <td>销售金额</td>
        </tr></thead>
            <tbody></tbody>
        </table>
    </div>
    </br>
    <div id="QuantityTableContainer">
        <table id='QuantityTable' class="tablesorter" onload="Quantitydata(6,6)" ><thead>
        <tr id='QuantityTableHead'>
            <td>销售数量</td>
        </tr></thead>
            <tbody></tbody>
        </table>
    </div>
    </br>
    <div id="profitTableContainer">
        <table id='profitTable' class="tablesorter" onload="profitdata(6,6)" ><thead>
        <tr id='profitTableHead'>
            <td>销售毛利</td>
        </tr></thead>
            <tbody></tbody>
        </table>
    </div>
    <div >
    </div>

    <!--content end-->

</block>